package com.onluy.frameworkcloud.batis.dao;


import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.onluy.frameworkcloud.batis.exception.ExportException;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.bean.Pagination;
import com.onluy.frameworkcloud.data.util.DatasetList;

public abstract class AbstractDao implements Dao {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    protected static final int DEFAULT_EXPORT_PAGE_SIZE = 2000;
    protected static final String UPLOAD_PATH = "temp/walkbatis_export_temp";

    protected <E> List<E> export(String statementId, Object param, int exportPageSize) {
        DatasetList dataset = new DatasetList();
        dataset.setSerializable(true);
        dataset.setBatchSerializable(true);
        dataset.setSerializablePath("temp/walkbatis_export_temp");
        dataset.setSerializableId(String.valueOf(System.currentTimeMillis()) + Math.abs((new Random()).nextInt()));
        File file = new File(dataset.getSerializablePath() + "/" + dataset.getSerializableId());
        if (!file.exists()) {
            file.mkdirs();
        }

        int i = 0;
        int pageCount = 0;

        while (true) {
            Pagination pagination = new Pagination();
            pagination.setNeedCount(false);
            pagination.setRange(i * exportPageSize, exportPageSize);
            PageData pageData = this.selectList(statementId, param, pagination);
            List subset = pageData.getRows();
            if (subset.size() <= 0) {
                break;
            }

            ++pageCount;

            try {
                String e = dataset.getSerializablePath() + "/" + dataset.getSerializableId() + "/" + i;
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(e)));
                out.writeObject(subset);
                out.close();
            } catch (Exception arg12) {
                throw new ExportException(arg12);
            }

            if (subset.size() < exportPageSize) {
                break;
            }

            ++i;
        }

        dataset.setBatchPageCount(pageCount);
        dataset.setBatchPageSize(exportPageSize);
        return dataset;
    }
}